package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hea7en
 * @Date: 2024/5/13 5:18 PM
 * @Description: 菜品管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理接口")
public class DishController {

    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO);
        dishService.addDishAndFlavor(dishDTO);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> pageQuery(DishPageQueryDTO pageQueryDTO){
        log.info("分页查询菜品:{}", pageQueryDTO);
        PageResult pageResult = dishService.pageQuery(pageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("批量删除菜品:{}", ids);
        dishService.deleteDish(ids);

        return Result.success();
    }

    /**
     * 启用停售菜品
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用停售菜品")
    public Result startOrStop(@PathVariable int status, Long id){
        log.info("启用停售菜品:{},{}", status, id);
        dishService.startOrStop(status, id);

        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据id查询菜品");
        DishVO dishVO = dishService.getDishById(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result editDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}", dishDTO);
        dishService.editDishAndFlavor(dishDTO);

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> getListByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品:{}", categoryId);
        List<DishVO> dishVOList = dishService.getListByCategoryId(categoryId);

        return Result.success(dishVOList);
    }
}
